import { Controller } from "../../mvc/core/Controller";
import { IVip360Controller } from "./interfaces/IVip360Controller";
import { Vip360Btn } from "../view/vip360/Vip360Btn";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { IView } from "../../mvc/interfaces/IView";
import { Vip360Module } from "../../../modules/Vip360Module";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { GameProxy } from "../mvc/GameProxy";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { ParamsConst } from "../../common/global/ParamsConst";
import { ClockManager } from "../manager/ClockManager";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SAttribute } from "../../../Message/Public/SAttribute";
import { VipGetState } from "../view/vip360/VipGetState";
import { PHPSender } from "../../../extend/php/PHPSender";
import { SVipGiftGagGet360 } from "../../../Message/Public/SVipGiftGagGet360";
import { IVip360Module } from "../../../modules/interfaces/IVip360Module";
type int = number;
//class Vip360Controller
    
    export  class Vip360Controller extends Controller implements IVip360Controller
    {
       

        public get vip360Btn(): Vip360Btn
        {
            if (this._vip360Btn == null) 
            {
                this._vip360Btn = new Vip360Btn();
                this._vip360Btn.addEventListener(EventName.Vip360WindowOpen, this.onVip360WindowOpenHandler);
            }
            return this._vip360Btn;
        }

        /* internal  */onVip360WindowOpenHandler(arg1: DataEvent): void
        {
            view.show();
            this._vip360Window.updateVipState();
            return;
        }

        protected /* override */ initView(): IView
        {
            if (this._vip360Window == null) 
            {
                this._vip360Window = new Vip360Module();
            }
            return this._vip360Window;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.Vip360GetGiftBag, this.onVip360GetGiftBagHandler);
            NetDispatcher.addCmdListener(ServerCommand.ActiveTypeVipGiftBag360, this.onActiveTypeVipGiftBag360Handler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublic360VipType, this.on360VipTypeHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublic360VipGiftBagGetList, this.on360VipGiftBagGetListHandler);
            return;
        }

        /* internal  */onVip360GetGiftBagHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.vip.getVipGiftBag360(loc1);
            return;
        }

        /* internal  */onActiveTypeVipGiftBag360Handler(arg1: SActiveToClient): void
        {
            this._activeToClient = arg1;
            var loc1=cache.active.isActiveInEffect(arg1.code);
            if (loc1 && this.vip360Btn.isHide && (!(ParamsConst.instance.vip360GetUrl == "") || !(ParamsConst.instance.vip360Url == ""))) 
            {
                this.vip360Btn.show();
                ClockManager.instance.addDateCall(arg1.endDt, this.removeVip360Active);
            }
            return;
        }

        /* internal  */removeVip360Active(): void
        {
            if (this._vip360Btn && !this._vip360Btn.isHide) 
            {
                this._vip360Btn.hide();
            }
            if (this._activeToClient) 
            {
                ClockManager.instance.removeDateCall(this._activeToClient.endDt);
            }
            return;
        }

        /* internal  */on360VipTypeHandler(arg1: MessageBlock): void
        {
            var loc2=null;
            var loc3=0;
            var loc1=arg1.messageBase as SAttribute;
            this._vipType = loc1.value;
            VipGetState.instance.curVipType = this._vipType;
            if (this._vip360Window && !this._vip360Window.isHide) 
            {
                this._vip360Window.updateVipState();
            }
            if (loc1.valueStr == "login") 
            {
                loc2 = ClockManager.instance.nowDate;
                if (loc2 == null) 
                {
                    loc2 = new Date();
                }
                loc3 = loc2.time / 1000;
                PHPSender.vipInfo360(loc3, this._vipType);
            }
            return;
        }

        /* internal  */on360VipGiftBagGetListHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SVipGiftGagGet360;
            VipGetState.instance.setVipHadGot(loc1.getList);
            if (this._vip360Window && !this._vip360Window.isHide) 
            {
                this._vip360Window.updateVipState();
            }
            return;
        }

        private /* var */_activeToClient: SActiveToClient;

        private /* var */_vip360Btn: Vip360Btn;

        private /* var */_vip360Window: IVip360Module;

        private /* var */_vipType: int=1;
    }
